import pymongo
import Two_sentiment#情感分析模型


#data_transform（）只有数据格式处理
#data_sentiment_op()只有情感分析处理，将没有分析的评论和文章进行分析后存入另一集合
#data_transformA()数据格式处理后直接情感分析处理


#只有数据格式处理
def data_transform(nFcomment, nFarticle, event_id):

    #test 事件id为1
    # event_id=1  #事件id

    client = pymongo.MongoClient("mongodb://localhost:27017/")

    cdb = client["crawler"] #爬虫数据库
    # cdb = client["local"] #爬虫数据库
    db = client["public_sentiment"] #情感分析数据库

    ######################################数据格式处理#################################

    c_comments = list(cdb[nFcomment].find())  # 未转换的评论
    c_articles = list(cdb[nFarticle].find())  # 未转换的文章

    # c_comments = list(cdb["test"].find()) #未转换的评论
    # c_articles = list(cdb["test1"].find()) #未转换的文章

    noemo_comments = [] #转换后的评论
    noemo_articles = [] #转换后的文章

    for i in c_comments:
        noemo_comments.append({
            "e_id": event_id,
            "user": i["nickname"],
            "content": i["content"],
            "like": int(i["comment_like_count"]),
            "time": i["create_date_time"]
        })

    for i in c_articles:
        noemo_articles.append({
            "e_id": event_id,
            "author": i["nickname"],
            "platform": "微博",
            "content": i["content"],
            "like": int(i["liked_count"]),
            "comments": int(i["comments_count"]),
            "share": int(i["shared_count"]),
            "link": i["profile_url"],
            "time": i["create_date_time"]
        })

    #将转换后数据存入情感分析数据库
    db["comments_noemo"].insert_many(noemo_comments)
    db["articles_noemo"].insert_many(noemo_articles)



#只有情感分析处理，将没有分析的评论和文章进行分析后存入另一集合
def data_sentiment_op():

    client = pymongo.MongoClient("mongodb://localhost:27017/")

    db = client["public_sentiment"]  # 情感分析数据库

    comments = list(db["comments_noemo"].find()) #取出没有分析的评论
    articles = list(db["articles_noemo"].find()) #取出没有分析的文章

    comments_content = [] #评论内容
    articles_content = [] #文章内容

    for i in comments:
        comments_content.append(i["content"])
    for i in articles:
        articles_content.append(i["content"])

    comments_emotion = Two_sentiment.get_emotion(comments_content) #评论情感分析结果
    articles_emotion = Two_sentiment.get_emotion(articles_content) #文章情感分析结果

    #情感分析结果的解析
    emo_comments = []
    emo_articles = []

    # 情感分析结果的解析----------下游任务
    sentimet_op(comments_emotion,emo_comments,comments)
    sentimet_op(articles_emotion,emo_articles,articles)

    #将分析结果存入数据库
    db['comments'].insert_many(comments)
    db['articles'].insert_many(articles)


#数据格式处理后直接情感分析处理
def data_transformA(nFcomment, nFarticle, event_id):
    # test 事件id
    # event_id = 1  # 事件id

    client = pymongo.MongoClient("mongodb://localhost:27017/")

    cdb = client["crawler"]  # 爬虫数据库
    db = client["public_sentiment"]  # 情感分析数据库

    ######################################数据格式处理#################################

    c_comments = list(cdb[nFcomment].find())  # 未转换的评论
    c_articles = list(cdb[nFarticle].find())  # 未转换的文章

    # c_comments = list(cdb["test"].find())  # 未转换的评论
    # c_articles = list(cdb["test1"].find())  # 未转换的文章

    noemo_comments = []  # 转换后的评论
    noemo_articles = []  # 转换后的文章

    for i in c_comments:
        noemo_comments.append({
            "e_id": event_id,
            "user": i["nickname"],
            "content": i["content"],
            "like": int(i["comment_like_count"]),
            "time": i["create_date_time"]
        })

    for i in c_articles:
        noemo_articles.append({
            "e_id": event_id,
            "author": i["nickname"],
            "platform": "微博",
            "content": i["content"],
            "like": int(i["liked_count"]),
            "comments": int(i["comments_count"]),
            "share": int(i["shared_count"]),
            "link": i["profile_url"],
            "time": i["create_date_time"]
        })

    ######################################情感分析#################################

    comments_content = []  # 评论内容
    articles_content = []  # 文章内容

    for i in noemo_comments:
        content = i.get("content")
        if content is None:
            print("在处理noemo_comments的时候缺失content：",i)
        comments_content.append(content)
        
        # comments_content.append(i["content"])

    for i in noemo_articles:
        content = i.get("content")
        if content is None:
            print("在处理noemo_articles的时候缺失content",i)
        articles_content.append(content)

        # articles_content.append(i["content"])

    comments_emotion = Two_sentiment.get_emotion(comments_content)  # 评论情感分析结果
    articles_emotion = Two_sentiment.get_emotion(articles_content)  # 文章情感分析结果

    # 情感分析结果的解析
    emo_comments = []
    emo_articles = []

    # 情感分析结果的解析----------下游任务
    sentimet_op(comments_emotion, emo_comments, noemo_comments)
    sentimet_op(articles_emotion, emo_articles, noemo_articles)

    # 将转换后数据存入情感分析数据库
    db["comments"].insert_many(noemo_comments)
    db["articles"].insert_many(noemo_articles)
    
    # 事件统计
    events_statistics(event_id,c_articles,c_comments,noemo_articles,noemo_comments)
#情感分析结果的解析
def sentimet_op(emotion,emo,items):
    for i in emotion:
        if i['labels'][0] == '正面':
            emo.append(round(i['scores'][0], 2))
        else:
            emo.append(round(i['scores'][1], 2))
    for i in range(len(items)):
        if emo[i] < 0.48:
            items[i]['sentiment'] = '消极'
            items[i]['intensity'] = (emo[i] - 0.5) * 2
        elif emo[i] > 0.52:
            items[i]['sentiment'] = '积极'
            items[i]['intensity'] = (emo[i] - 0.5) * 2
        else:
            items[i]['sentiment'] = '中性'
            items[i]['intensity'] = (emo[i] - 0.5) * 2

#事件统计----目前只统计了部分数据
def events_statistics(event_id,articlesG,commentsG,articlesE,commentsE):
    client = pymongo.MongoClient("mongodb://localhost:27017/")
    gender_m=0
    gender_f=0
    positive=0
    neutral=0
    negative=0
    sentiment=""
    db=client["public_sentiment"]
    for i in articlesG:
        if (i["gender"] == "m"): gender_m += 1
        else: gender_f += 1
    for i in commentsG:
        if (i["gender"] == "m"): gender_m += 1
        else: gender_f += 1
    for i in articlesE:
        if (i["sentiment"] == "积极"): positive += 1
        if (i["sentiment"] == "消极"): negative += 1
        if (i["sentiment"] == "中性"): neutral += 1
    for i in commentsE:
        if (i["sentiment"] == "积极"): positive += 1
        if (i["sentiment"] == "消极"): negative += 1
        if (i["sentiment"] == "中性"): neutral += 1
    if (neutral>positive and neutral>negative): sentiment="中性"
    elif (positive>negative): sentiment="积极"
    else: sentiment="消极"
    db["events"].update_one({"id":event_id},{"$set":{"genderData":[gender_f,gender_m],"sentiment":sentiment,"sentimentData":[positive,neutral,negative]}})



if __name__ == "__main__":
    # data_sentiment_op()
    # data_transform("wb_小米汽车_comments_20250515_234054","wb_小米汽车_contents_20250515_234054",1)
    data_transformA("wb_高考_comments_20250517_185423","wb_高考_contents_20250517_185423",1)